<?php

declare(strict_types=1);

namespace app\common\traits;

/**
 * 流程控制器层公共方法
 * @author kaige
 * @update 2022-08-27
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
trait FlowControllerTrait
{

  /**
   * 表单编辑、查看时 输出数据格式化
   */
  public function _form_outformat(&$data)
  {
  }

  /**
   * 列表之前--可传参
   */
  public function _index_before(&$data)
  {
  }
  /**
   * 列表之后--可格式化数据
   */
  public function _index_after(&$outlist,$data)
  {
  }

  /**
   * 列表
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      url_decode($data);
      //过滤 数据权限
      // 主表 put_id 申请人id 
      $data['user_id'] = $this->user['id'];
      $this->_index_before($data);
      // $data['alias'] = Aalias($this->table_name);
      $outlist = $this->service->_flow_list($data);
      $outlist['flow_config'] =  (new \app\common\service\pmm\ConfigService())->getInfoById($this->table_moduId); //取流程配置
      //手动模式，删除表单配置
      if ($outlist['flow_config']['form_type'] == 2) {
        unset($outlist['flow_config']['form_json']);
      }
      $this->_index_after($outlist,$data);
      return json($outlist);
    }
    return _jerr(1000);
  }

  /**
   * 添加
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      $this->_form_before($data);
      if ($this->_is_exists_record($data, $this->user)) {
        return _jerr('存在相同记录');
      }
      $result = $this->service->saveData($data, $this->user, $this->sysdict);
      return json($result);
    }
    if ($this->request->isGet()) {
      $data = [
        'einfo' => (new \app\common\service\sys\UserService())->getEmployeeInfo($this->user['id']),
        'fi' => [],
        'flowstep' => (new \app\common\service\pmm\FlowStepService())->getListAll(['modu_id' => $this->table_moduId], ['sort' => 'asc', 'id' => 'asc'])
      ];
      $this->_add_get_before($data);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加get之前
   */
  public function _add_get_before(&$data)
  {
    
  }

  /**
   * 编辑
   */
  public function edit()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      $this->_form_before($data);
      $result = $this->service->saveData($data, $this->user, $this->sysdict);
      return json($result);
    }
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $this->service->getflow($id);
      //判断是不是操作人
      if ($data['handle_uid'] != $this->user['id']) {
        return _jerr('不能编辑他人的流程');
      }
      $data['einfo'] = (new \app\common\service\sys\UserService())->getEmployeeInfo($this->user['id']);
      $data['fi'] = \app\common\service\hrm\FilesService::getFilesJson(['id' => $id, 'table_name' => $this->table_name]);
      $data['flowstep'] = (new \app\common\service\pmm\FlowStepService())->getListAll(['modu_id' => $this->table_moduId], ['sort' => 'asc', 'id' => 'asc']);
      $this->_form_outformat($data);
      $this->_edit_get_before($data);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 查看
   */
  public function look()
  {
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $this->service->getflow($id);
      $data['einfo'] = (new \app\common\service\sys\UserService())->getEmployeeInfo($this->user['id']);
      $data['fi'] = \app\common\service\hrm\FilesService::getFilesJson([
        'id' => $id,
        'table_name' => $this->table_name
      ]);
      $fsls = new \app\common\service\pmm\FlowStepLogService();
      //取流程步骤
      $data['flowstep'] = $fsls->getListAll(['modu_id' => $this->table_moduId, 'm_id' => $id], ['sort' => 'asc', 'step_id' => 'asc']);
      //流程副本步骤
      $data['nowstep'] = $fsls->getInfoByWhere(['modu_id' => $this->table_moduId, 'm_id' => $id, 'step_id' => $data['now_step_id']]);
      //取流程日志
      $data['flowlog'] = (new \app\common\service\pmm\FlowLogService())->getListAll([['modu_id', '=', $this->table_moduId], ['m_id', '=', $id], ['step_id', '>', 0]]);
      //浏览记录
      (new \app\common\service\pmm\FlowBrowsingService())->viewhandle(['modu_id' => $this->table_moduId, 'm_id' => $id, 'table' => $this->table_name], $this->user);
      //过滤掉用户其它信息
      $showuser = ['id','un','avatar','dept_id','dept_id_name','ranking_id','ranking_id_name','real_name','company_id','theme','sign'];
      $userdata = [];
      foreach($this->user as $k=>$v){
        if(in_array($k,$showuser)){
          $userdata[$k] = $v;
          if($k == 'avatar'){
            $userdata['avatar'] = _img($v);
          }
        }
      }
      $data['user'] = $userdata;
      $this->_form_outformat($data);
      $this->_look_get_before($data);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 过滤是否存在记录
   */
  public function _is_exists_record($data, $user)
  {
    return false;
  }

  /**
   * 查看get之前
   */
  public function _look_get_before(&$data)
  {
  }
  /**
   * 编辑get之前
   */
  public function _edit_get_before(&$data)
  {
  }
  /**
   * 添加、编辑之前 -- 数据处理
   */
  public function _form_before(&$data)
  {
  }

  /**
   * 添加、编辑之前 --公共数据处理
   * @param array $data
   */
  protected function _form_filter(&$data)
  {
    url_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    if (isset($data['id']) && !empty($data['id'])) {
      //编辑
    } else {
      //添加
      $data['put_time'] = (isset($data['put_time']) && !empty($data['put_time'])) ? strtotime($data['put_time']) : time();
      $data['handle_time'] = (isset($data['handle_time']) && !empty($data['handle_time'])) ? strtotime($data['handle_time']) : time();
      if (isset($data['put_uid']) && !empty($data['put_uid'])) {
        $data['put_uid'] = $data['put_uid'];
        $data['put_name'] = $data['put_name'];
        $data['put_dept_id'] =  $data['put_dept_id'];
        $data['put_dept_id_name'] = $data['put_dept_id_name'];
        $data['put_ranking_id'] =  $data['put_ranking_id'];
        $data['put_ranking_id_name'] = $data['put_ranking_id_name'];
      } else {
        $data['put_uid'] = $this->user['id'];
        $data['put_name'] = $this->user['real_name'];
        $data['put_dept_id'] = $this->user['dept_id'];
        $data['put_dept_id_name'] = $this->user['dept_id_name'];
        $data['put_ranking_id'] = $this->user['ranking_id'];
        $data['put_ranking_id_name'] = $this->user['ranking_id_name'];
      }
      $data['handle_uid'] = $this->user['id'];
      $data['handle_name'] = $this->user['real_name'];
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      if ($this->service->deleteFlow($id, $this->user)) {
        return _jok(1025);
      };
      return _jerr(1026);
    }
    return _jerr(1000);
  }

  /**
   * 审批处理
   */
  public function auditstatus()
  {
    if ($this->request->isPost()) {
      $data = $this->request->post();
      url_decode($data);
      $fls = new \app\common\service\pmm\FlowLogService();
      $result = $fls->auditstatus($data, $this->user, $this->sysdict);
      if($result['status']){//审核通过时
        //判断是否最后一个步骤，审核通过时，即流程结束时 通过状态。执行回调
        //因不同模块有不同的回调，故不能放在flowlogservice里做回调，只能放在各自模块业务层里。
        $this->service->_audit_allok($result['data'],$this->user); //逻辑代码放各自模块 业务层里。
      }
      return json($result);
    }
    return _jerr(1000);
  }

}
